package cn.yingtian.image;

import java.util.List;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter{
	private List<Content> contents;
	private LayoutInflater inflater;
	private int resourceId;
	private ImageLoader loader=ImageLoader.newInstance();
	public ImageAdapter(List<Content> contents, Context context,
			int resourceId) {
		this.contents = contents;
		inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.resourceId = resourceId;
	}

	@Override
	public int getCount() {
		return contents.size();
	}

	@Override
	public Object getItem(int position) {
		return contents.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position,  View convertView, ViewGroup parent) {
		if(convertView==null){
			convertView=inflater.inflate(resourceId, null);
		}
		Content con=contents.get(position);
		TextView desc = (TextView) convertView.findViewById(R.id.description);
		TextView name = (TextView) convertView.findViewById(R.id.name);
		final ImageView img = (ImageView) convertView.findViewById(R.id.img);
		System.out.println(con.getUrl());
		img.setTag(con.getUrl());
		desc.setText(con.getDescription());
		name.setText(con.getName());
		loader.loadImage(con.getUrl(), new ImageLoaderCallBack() {
			
			@Override
			public void imageLoaded(Uri uri, String imageUrl) {
				ImageView view=	(ImageView) img.findViewWithTag(imageUrl);
				view.setImageURI(uri);
			}
		});
		return convertView;
	}

}
